package com.jh3502.helper.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.jh3502.helper.Jh3502HelperApplication;
import com.jh3502.helper.utils.eventbus.EventBusMessage;
import com.jh3502.helper.view.SnackbarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;

import static org.greenrobot.eventbus.EventBus.TAG;

/**
 * @author harisucici
 * @date 07.03
 */
public abstract class BaseFragment extends Fragment implements BaseView {
    public Jh3502HelperApplication mJh3502HelperApplication;
    @Nullable
    public Context mContext;
    @Nullable
    public BaseActivity mActivity;
    public boolean isVisible, isPrepared;
    @Nullable
    protected ProgressDialog mProgressDialog;
    protected View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mJh3502HelperApplication = (Jh3502HelperApplication) getActivity().getApplication();
        if (rootView == null) {
            mContext = getContext();
            mActivity = (BaseActivity)getActivity();
            rootView = inflater.inflate(setViewId(), container, false);
            isPrepared = true;
            ButterKnife.bind(this, rootView);
            EventBus.getDefault().register(this);
            initView();
            initData();

        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = getUserVisibleHint();
        if (!isPrepared || !isVisible) {
            return;
        }
        if (isPrepared) {
            if (getUserVisibleHint()) {
                isVisible = true;
                doOnVisible();
            } else {
                isVisible = false;
                doOnInvisible();
            }
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        doOnResume();
        Log.i("BaseFragment_Me", "onResume");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        doOnDestroyView();
        EventBus.getDefault().unregister(this);
//        bind.unbind();
        Log.i("BaseFragment_Me", "onDestroyView");
    }


    protected abstract int setViewId();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void doOnDestroyView();

    protected abstract void doOnResume();

    protected abstract void doOnVisible();

    protected abstract void doOnInvisible();

    protected abstract void doOnSaveInstanceState();

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        doOnSaveInstanceState();
    }

    @Override
    public void showLoadingDialog() {
        showLoadingDialog(null, null);
    }


    @Override
    public void showLoadingDialog(String msg) {
        showLoadingDialog(null, msg);
    }

    public void showLoadingDialog(@Nullable String title, @Nullable String message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }

        if (title == null) {
            title = "";
        }
        if (message == null) {
            message = "";
        }

        mProgressDialog.setTitle(title);
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
    }


    @Override
    public void dismissLoadingDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Subscribe
    public void onEventMainThread(@NonNull EventBusMessage eventBusMessage) {
        Log.i(TAG, "onEventMainThread: " + eventBusMessage.toString());
    }

    @Nullable
    @Override
    public Context getActContext() {
        return getContext();
    }


    public void showShortToast(@NonNull String message) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
        }
        SnackbarUtil.showBarShortTime(getActivity().findViewById(android.R.id.content), message, SnackbarUtil.INFO);
    }

    public void showShortToast(@NonNull String message, int type) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
        }
        SnackbarUtil.showBarShortTime(getActivity().findViewById(android.R.id.content), message, type);
    }

    protected void showShortToast(@NonNull String message, @NonNull View.OnClickListener listener, String info) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
        }
        SnackbarUtil.showBarShortTime(getActivity().findViewById(android.R.id.content), message, SnackbarUtil.INFO, info, listener);
    }

}
